package com.midea.ai.air.view;

import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;

import com.midea.ai.air.R;

/**
 * Created by ouyangdi on 2016/3/30.
 * 基础列表View
 */
public class ListItemView extends FrameLayout{
    private ImageView mIvIcon;
    private TextView mTvName;
    private View mBotLine;

    public ListItemView(Context context) {
        this(context, null, 0);
    }

    public ListItemView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public ListItemView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);

        View.inflate(context, R.layout.list_item_more, this);
        initView();

        TypedArray array = context.obtainStyledAttributes(attrs, R.styleable.ListItemView);
        int visibility_icon = array.getInt(R.styleable.ListItemView_icon_visibility, 2);
        int visibility_bot = array.getInt(R.styleable.ListItemView_bot_line_visibility, 2);
        int resId = array.getResourceId(R.styleable.ListItemView_icon_src, R.drawable.air_more_integral);
        String strName = array.getString(R.styleable.ListItemView_text_name);
        array.recycle();

        setViewVisibility(mIvIcon, visibility_icon);
        if(mIvIcon.getVisibility() == View.VISIBLE) {
            mIvIcon.setImageResource(resId);
        }

        setViewVisibility(mBotLine, visibility_bot);
        mTvName.setText(strName);
    }

    private void setViewVisibility(View view,int visibility) {
        switch (visibility) {
            case 0:
                view.setVisibility(View.VISIBLE);
                break;
            case 1:
                view.setVisibility(View.INVISIBLE);
                break;
            case 2:
                view.setVisibility(View.GONE);
                break;
        }
    }

    private void initView() {
        mIvIcon = (ImageView)findViewById(R.id.imgView_icon);
        mTvName = (TextView)findViewById(R.id.txtView_name);
        mBotLine = findViewById(R.id.bottom_line);
    }
}
